home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / PAS_0793 / QWKTIMER.PAS < prev    next >
Pascal/Delphi Source File  |  1993-08-01  |  4KB  |  138 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 291 of 295
  3. From : Sam Leventer                        1:2613/313.0         02 Jul 93  12:29
  4. To   : Kent Briggs
  5. Subj : Interrupt driven timer
  6. ────────────────────────────────────────────────────────────────────────────────}
  7. Unit QwkTimer;
  8.  
  9. {
  10.  *********************************************************************
  11.  **                                                                 **
  12.  ** Author....: Chris Wood                                          **
  13.  ** Date......: 7-11-1992                                           **
  14.  ** Internet..: woodc@jacobs.cs.orst.edu                            **
  15.  ** BBS.......: A Separate Reality (503)926-6991                    **
  16.  **             (Leave mail to the sysop)                           **
  17.  **                                                                 **
  18.  **   This source code is being released as Free-Ware.  You may use **
  19.  ** this code in your programs and modify it to fit your needs. The **
  20.  ** only  restrictions are that you  may not distribute the  source **
  21.  ** code in modified form or charge for the source code itself.  If **
  22.  ** you  discover any  errors  in the  source code  or find a  more **
  23.  ** efficient way to handle any of the following code, please leave **
  24.  ** me e-mail at the internet address or the BBS listed above.  All **
  25.  ** comments are welcome.                                           **
  26.  **                                                                 **
  27.  ********************************************************************* }
  28.  
  29. {$O+}
  30. {$N+}
  31. {$E+}
  32.  
  33. Interface
  34.  
  35. Var ClockTicks : LongInt;  { Number of ticks elapsed }
  36.  
  37. Procedure TimerOn ( Freq : LongInt );
  38. Procedure TimerOff;
  39. Procedure ResetTimer;
  40. Function TimeElapsed : Real;
  41.  
  42. Implementation
  43.  
  44. Uses CRT,DOS;
  45.  
  46. Const MaxRate = 1193180;
  47.  
  48. Var OldInt08        : Procedure;
  49.     OldInt1C        : Procedure;
  50.     IntCount08      : Word;
  51.     Trigger         : Word;
  52.     TimerAlreadySet : Boolean;
  53.     Frequency       : Word;
  54.  
  55. Procedure IrqOn; Inline($FB);
  56.  
  57. Procedure IrqOff; Inline($FA);
  58.  
  59. {$F+}
  60. Procedure NewInt1C; Interrupt;
  61. Begin
  62.   Inc(ClockTicks);
  63. End;
  64. {$F-}
  65.  
  66. {$F+}
  67. Procedure NewInt08; Interrupt;
  68. Begin
  69.   IrqOff;
  70.   Inline($CD/$1C); {Generate INT 1Ch instruction to call interrupt 1Ch}
  71.   If IntCount08=Trigger then
  72.   Begin
  73.     IntCount08:=0;
  74.     Inline($9C);
  75.     OldInt08;
  76.   End
  77.     Else
  78.   Begin
  79.     Inc(IntCount08);
  80.   End;
  81.   Port[$20]:=$20; {Sends non-specific EOI to the PIC}
  82.   IrqOn;
  83. End;
  84. {$F-}
  85.  
  86. Procedure TimerOn ( Freq : LongInt );
  87. Var Temp  : LongInt;
  88.     Count : Word;
  89. Begin
  90.   If Not(TimerAlreadySet) then
  91.   Begin
  92.     ClockTicks:=0;
  93.     IntCount08:=0;
  94.     Frequency:=Freq;
  95.     Trigger:=Trunc(Freq/18.2);
  96.     Temp:=MaxRate;
  97.     Temp:=Trunc(Temp/Freq);
  98.     Count:=Temp;
  99.     GetIntVec($08,@OldInt08);
  100.     SetIntVec($08,Addr(NewInt08));
  101.     GetIntVec($1C,@OldInt1C);
  102.     SetIntVec($1C,Addr(NewInt1C));
  103.     Port[$43]:=$B6;
  104.     Port[$40]:=Lo(Count);
  105.     Port[$40]:=Hi(Count);
  106.     TimerAlreadySet:=True;
  107.   End;
  108. End;
  109.  
  110. Procedure TimerOff;
  111. Begin
  112.   If TimerAlreadySet then
  113.   Begin
  114.     Port[$43]:=$B6;
  115.     Port[$40]:=$FF;
  116.     Port[$40]:=$FF;
  117.     SetIntVec($08,@OldInt08);
  118.     SetIntvec($1C,@OldInt1C);
  119.     TimerAlreadySet:=False;
  120.   End;
  121. End;
  122.  
  123. Procedure ResetTimer;
  124. Begin
  125.   ClockTicks:=0;
  126. End;
  127.  
  128. Function TimeElapsed : Real;
  129. Begin
  130.   TimeElapsed:=ClockTicks/Frequency;
  131. End;
  132.  
  133. Begin
  134.   TimerAlreadySet:=False;
  135. End.
  136.  
  137. {Note: for best results call timeron with a multiple of 18.2; for example 1820 
  138. for 100 timers accuracy}